package com.ppt.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.ppt.dao.Empleados;
import com.ppt.dao.EmpleadosHome;
import com.ppt.util.VariablesCompartidas;

@Entity
public class LoginFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

//	private MainApp base;

	@ManyToOne
	private EmpleadosHome empleadosHome;
	@ManyToOne
	private Empleados empleado;
	
	private JTextField txtCodEmpleado;
	private JTextField txtPass;

	private JButton btnEnviar;
	private JButton btnSalir;

	public LoginFrame() {
		super();
//		this.base = base;
		initializeGUI();
		addListeners();
	}

	public void initializeGUI() {
		BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
		borderLayout.setVgap(5);
		borderLayout.setHgap(5);
		setResizable(false);

		setTitle("Identificaci�n");
//		setModal(true);
		setSize(320, 250);
//		setLocationRelativeTo(base);
		setLocationRelativeTo(null);

		JPanel panelNorth = new JPanel();
		getContentPane().add(panelNorth, BorderLayout.NORTH);

		JLabel lblBienvenidoAPpt = new JLabel("Bienvenido a PPT!");
		lblBienvenidoAPpt.setForeground(Color.RED);
		lblBienvenidoAPpt.setFont(new Font("Monospaced", Font.BOLD
				| Font.ITALIC, 16));
		panelNorth.add(lblBienvenidoAPpt);

		JPanel panelCenter = new JPanel();
		getContentPane().add(panelCenter, BorderLayout.CENTER);
		GridBagLayout gbl_panelCenter = new GridBagLayout();
		gbl_panelCenter.columnWidths = new int[] { 150, 150, 0 };
		gbl_panelCenter.rowHeights = new int[] { 50, 50, 0 };
		gbl_panelCenter.columnWeights = new double[] { 1.0, 1.0,
				Double.MIN_VALUE };
		gbl_panelCenter.rowWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
		panelCenter.setLayout(gbl_panelCenter);

		JLabel lblCdigoEmpleado = new JLabel("C\u00F3digo Empleado:");
		GridBagConstraints gbc_lblCdigoEmpleado = new GridBagConstraints();
		gbc_lblCdigoEmpleado.anchor = GridBagConstraints.SOUTHEAST;
		gbc_lblCdigoEmpleado.insets = new Insets(0, 0, 10, 5);
		gbc_lblCdigoEmpleado.gridx = 0;
		gbc_lblCdigoEmpleado.gridy = 0;
		panelCenter.add(lblCdigoEmpleado, gbc_lblCdigoEmpleado);

		txtCodEmpleado = new JTextField();
		GridBagConstraints gbc_txtCodEmpleado = new GridBagConstraints();
		gbc_txtCodEmpleado.anchor = GridBagConstraints.SOUTHWEST;
		gbc_txtCodEmpleado.insets = new Insets(0, 5, 10, 0);
		gbc_txtCodEmpleado.gridx = 1;
		gbc_txtCodEmpleado.gridy = 0;
		panelCenter.add(txtCodEmpleado, gbc_txtCodEmpleado);
		txtCodEmpleado.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblPassword.insets = new Insets(10, 0, 0, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 1;
		panelCenter.add(lblPassword, gbc_lblPassword);

		txtPass = new JTextField();
		GridBagConstraints gbc_txtPass = new GridBagConstraints();
		gbc_txtPass.insets = new Insets(10, 5, 0, 0);
		gbc_txtPass.anchor = GridBagConstraints.NORTHWEST;
		gbc_txtPass.gridx = 1;
		gbc_txtPass.gridy = 1;
		panelCenter.add(txtPass, gbc_txtPass);
		txtPass.setColumns(10);

		JPanel panelSouth = new JPanel();
		getContentPane().add(panelSouth, BorderLayout.SOUTH);
		panelSouth.setLayout(new BorderLayout(0, 0));

		JPanel panelSouthLeft = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panelSouthLeft.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panelSouth.add(panelSouthLeft, BorderLayout.WEST);

		btnSalir = new JButton("Salir");
		panelSouthLeft.add(btnSalir);

		JPanel panelSouthRight = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panelSouthRight.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panelSouth.add(panelSouthRight, BorderLayout.EAST);

		btnEnviar = new JButton("Enviar");
		panelSouthRight.add(btnEnviar);
		setVisible(true);
//		pack();
	}

	private void addListeners() {
		btnSalir.addActionListener(this);
		btnEnviar.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSalir) {
			System.out.println("Salir");
			System.exit(0);
		}
		if (e.getSource() == btnEnviar) {
			System.out.println("Enviar");
			if (comprobarEmpleado()){
				this.dispose();
				
				MainFrame mainFrame = new MainFrame();
				mainFrame.setVisible(true);
			}
			
		}
	}
	
	public boolean comprobarEmpleado(){
		empleadosHome = new EmpleadosHome();
		String code = txtCodEmpleado.getText();
		String pass = txtPass.getText();
		
		try {
//			empleado = empleadosHome.findByCodeAndPass(code, pass);
			empleado = empleadosHome.findById(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (empleado != null){
			System.out.println("Nombre Empleado: "+empleado.getNombre());
			return true;
		}
		else return false;
		
//		Empleados em = new Empleados(8888, "8033", "carlos", "Carlos", "Molina Robles", "93", 0);
//		VariablesCompartidas.empleado = em;
//		VariablesCompartidas.operario = true;
//		VariablesCompartidas.tipoDeclaracion = 1;
//		return true;
	}
}
